﻿function returnBlank(str){
    if(str == null || str == "")
        return " "
    else
        return str
}

layui.use(['table','form'], function(){
    var form = layui.form;
    form.on('submit(sendData)', function(data){
        layer.confirm('确认修改?', {icon: 3, title:'警告'}, function(index) {
            var a=GetRequest();
            var tableid=a['tableId'];
            var typeid = a['typeId'];
            let funnameTemp = new Array();
            let funmsgTemp = new Array();
            var funnum = $('#fun_num').val();
            if(funnum > 0)  {
                for(let i = 1; i <= funnum; i++) {
                    funnameTemp.push(returnBlank($('#fun_name_'+i).val()));
                    funmsgTemp.push(returnBlank($('#fun_msg_'+i).val()));
                }
            }
            let funname = funnameTemp.join('__');
            let funmsg = funmsgTemp.join('//');

            $.ajax({
                type:'post',
                dataType: "json",
                contentType: "application/json",
                url:"/contract/saveSystem2Function",
                data:
                    JSON.stringify({
                        level1SystemId: tableid,
                        typeId: typeid,
                        funNum : funnum,
                        funName: funname,
                        funMsg: funmsg
                    }),
                success: function (res) {
                    if(res.msg == 'success') {
                        layer.msg("修改成功！ 页面即将关闭！ ");
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        setTimeout(
                            function(){
                                parent.layer.close(index);//关闭弹出的子页面窗口*!/
                            },1000)
                    } else {
                        layer.msg('修改失败');
                    }
                }
            });
            layer.close(index);
        });
        return false;
    });
});


function initData(tableid, typeid, status){

    $.ajax({
        type:'get',
        dateType: "json",
        url:"/contract/findSystem2Function",
        data:{
                level1SystemId: tableid,
                typeId: typeid
        },
        success:function(res){
            console.log(res);
            if(res.msg == 'success') {
                setData(res.data.system2Function, status);
            }
        }
    });
}
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
function setData(info,status){
    $('#fun_num').val(info.funNum);
    setTimeout(
        function(){
            var evtObj = document.createEvent('UIEvents');
            evtObj.initUIEvent('input', true, true, window, 1 );
            setFunmsg(info, evtObj);
            setDisabled(status);
    });
}
function setFunmsg(info, evtObj){
    document.querySelector("#fun_num").dispatchEvent(evtObj);
    var funnum = $('#fun_num').val();
        if(funnum > 0)  {
        var funname = info.funName.split('__');
        var funmsg = info.funMsg.split('//');
        for(let i = 1; i <= funnum; i++) {
            $('#fun_name_'+i).val(funname[i-1]);
            $('#fun_msg_'+i).val(funmsg[i-1]);
        }
    }
}

function setDisabled(status){
    if(status==1) {
        $('#button_store').hide();
        $('#button_store').hide();
        $('#fun_num').attr("disabled","disabled");
        for (let i =1;i<=$('#fun_num').val();++i) {
            $('#fun_name_'+i).attr("disabled","disabled");
            $('#fun_msg_'+i).attr("disabled","disabled");
        }
    }
}

function setMode(){
    $('#fun_num').bind('input propertychange',function() {
        if( $('input[name="fun_num"]').val() == "")  $('input[name="fun_num"]').val(0)
        var text = $('input[name="fun_num"]').val();
        var nowNum = parseInt(text);
        var hiddenNum = parseInt($('#fun_hide_num').val());
        if(nowNum > hiddenNum) {
            $('#fun_hide_num').val(text);
            for (let i =1;i<=text;++i){
                if($('#fun_'+i).length <= 0) {
                    $("<li id='fun_"+i+"'><div class='layui-form-item'><label for='username' class='layui-form-label' style='color: red;' >*核心项目"+i+
                        " ： </label></div> <div class='layui-form-item'><label for='username' class='layui-form-label'>项目名称"+
                        "</label><div class='layui-input-inline'><input name='fun[]' type='text' id='fun_name_"+i+
                        "' autocomplete='off' class='layui-input'></div></div> <div class='layui-form-item'>" +
                        "<label for='username' class='layui-form-label'>功能描述</label>"+
                        " <div class='layui-input-inline'> <textarea  id='fun_msg_"+i+
                        "' name='fun_msg[]' style='width: 500px;' class='layui-input'></textarea></div></div></li>").appendTo($('#fun_upload'));
                    }
                }
        }else {
            for (let i = hiddenNum;i>nowNum;--i) {
                $('#fun_'+i).remove();
            }
            $('#fun_hide_num').val(text);
        }});
}

